package BandB.Tool.GPhoneManager.TaskManager.config;

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

import android.util.Log;

public class LogManager {
	private static LogManager mLogManager;
	private static final String configFileName = "config.txt";
	private static final String TAG = "LogManager";

	private LogManager() {

	}

	public synchronized static LogManager getInstance() {
		if (null == mLogManager) {
			mLogManager = new LogManager();
		}
		return mLogManager;
	}

	public synchronized static void writeConfig(long mills) throws IOException {
		File file = new File(configFileName);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileWriter fw = new FileWriter(file);
		String millsString = "\n" + String.valueOf(mills);
		fw.write(millsString, 0, millsString.length());
		fw.flush();
		fw.close();
		// Log.e(TAG, "can't create " + configFileName + " file");
	}

	public synchronized static long readConfig() throws IOException {
		RandomAccessFile rf = new RandomAccessFile(configFileName, "r");
		long len = rf.length();
		long start = rf.getFilePointer();
		long next_end = start + len - 1;
		rf.seek(next_end);
		String lastConfig = rf.readLine();
		long config = -1;
		try {
			config = Long.parseLong(lastConfig);
		} catch (Exception ex) {
			Log.e(TAG, "readConfig failed." + ex.getMessage());
		}
		return config;
	}
}
